package org.jeecg.modules.website.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.website.entity.WebWorkGuide;
import org.jeecg.modules.website.entity.WebWorkFile;
import org.jeecg.modules.website.mapper.WebWorkFileMapper;
import org.jeecg.modules.website.mapper.WebWorkGuideMapper;
import org.jeecg.modules.website.service.IWebWorkGuideService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;

import java.util.List;
import java.util.Collection;

/**
 * @Description: 办事指南表
 * @Author: jeecg-boot
 * @Date:   2024-07-09
 * @Version: V1.0
 */
@Service
public class WebWorkGuideServiceImpl extends ServiceImpl<WebWorkGuideMapper, WebWorkGuide> implements IWebWorkGuideService {

	@Autowired
	private WebWorkGuideMapper webWorkGuideMapper;
	@Autowired
	private WebWorkFileMapper webWorkFileMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(WebWorkGuide webWorkGuide, List<WebWorkFile> webWorkFileList) {
		webWorkGuideMapper.insert(webWorkGuide);
		if(webWorkFileList!=null && webWorkFileList.size()>0) {
			for(WebWorkFile entity:webWorkFileList) {
				//外键设置
				entity.setGuideId(webWorkGuide.getId());
				webWorkFileMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(WebWorkGuide webWorkGuide,List<WebWorkFile> webWorkFileList) {
		webWorkGuideMapper.updateById(webWorkGuide);
		
		//1.先删除子表数据
		webWorkFileMapper.deleteByMainId(webWorkGuide.getId());
		
		//2.子表数据重新插入
		if(webWorkFileList!=null && webWorkFileList.size()>0) {
			for(WebWorkFile entity:webWorkFileList) {
				//外键设置
				entity.setGuideId(webWorkGuide.getId());
				webWorkFileMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		webWorkFileMapper.deleteByMainId(id);
		webWorkGuideMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			webWorkFileMapper.deleteByMainId(id.toString());
			webWorkGuideMapper.deleteById(id);
		}
	}
	@Override
	public List<WebWorkGuide> findByCreateTimeRange(String startTime, String endTime) {
		QueryWrapper<WebWorkGuide> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().between(WebWorkGuide::getCreateTime, startTime, endTime);
		return baseMapper.selectList(queryWrapper);
	}
}
